using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Text.RegularExpressions;
using System.Globalization;

using Google.GData.Calendar;
using Google.GData.Client;
using Google.GData.Extensions;

namespace MyGoogleCalendar
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        ArrayList entryList = new ArrayList(100);

        private void btnLogin_Click(object sender, EventArgs e)
        {
            ArrayList dates = new ArrayList(50);
            ArrayList DayEvent = new ArrayList(100);
            string strUserName = txtUserName.Text.Trim();
            string strPwd = txtPwd.Text.Trim();
            CalendarService service = new CalendarService("MyGoogleCalendar");
            
            service.setUserCredentials(strUserName, strPwd);
            EventQuery query = new EventQuery();

            query.Uri = new Uri("http://www.google.com/calendar/feeds/default/private/full");
            query.StartTime = DateTime.Now.AddDays(-28);
            query.EndDate = DateTime.Now.AddDays(6);

            EventFeed calFeed = service.Query(query) as EventFeed;

            while (calFeed != null && calFeed.Entries.Count > 0)
            {
                foreach (EventEntry entry in calFeed.Entries)
                {
                    this.entryList.Add(entry);
                    if (entry.Times.Count > 0)
                    {
                        foreach (When w in entry.Times)
                        {
                            dates.Add(w.StartTime);
                        }
                    }
                }
                if (calFeed.NextChunk != null)
                {
                    query.Uri = new Uri(calFeed.NextChunk);
                    calFeed = service.Query(query) as EventFeed;
                }
                else
                    calFeed = null;
            }

            ArrayList listMonth = new ArrayList();
            DateTime startDate = DateTime.Now.AddDays(0-DateTime.Now.Day);
            DateTime endDate = DateTime.Now.AddMonths(1).AddDays(-1);
            DateTime tmp = startDate;

            while( tmp < endDate)
            {
                listMonth.Add(tmp);
                tmp = tmp.AddDays(1);
            }

            ArrayList result = new ArrayList(200);

            foreach (EventEntry entry in this.entryList)
            {
                if (entry.Times.Count > 0)
                {
                    foreach (When w in entry.Times)
                    {
                        foreach( DateTime d in listMonth)
                        {
                            if (d.Date == w.StartTime.Date || d.Date == w.EndTime.Date)
                            {
                                result.Add(entry);
                                break;
                            }
                        }
                    }
                }
            }

            StringBuilder sb = new StringBuilder();

            foreach (EventEntry entry in result)
            {
                sb.AppendFormat("Title:{0}\r\n", entry.Title.Text);
                sb.AppendFormat("\tAuthors:{0}\r\n", entry.Authors[0].Name);
                if (entry.Times.Count > 0)
                {
                    sb.AppendFormat("StartTime:{0}\r\n", entry.Times[entry.Times.Count-1].StartTime);
                    sb.AppendFormat("EndTime:{0}\r\n", entry.Times[entry.Times.Count - 1].EndTime);
                }
            }
            Console.WriteLine(sb.ToString());
            MessageBox.Show(sb.ToString());
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string strTest = textBox1.Text.Trim();
            MessageBox.Show( ToPascalCase(strTest) );
        }

        private string ToLowerExceptCamelCase(string input)
        {
            char[] chars = input.ToCharArray();
            for (int i = 0; i < chars.Length; i++)
            {
                int left = (i > 0 ? i - 1 : i);
                int right = (i < chars.Length - 1 ? i + 1 : i);
                if (i != left && i != right)
                {
                    if (Char.IsUpper(chars[i]) && Char.IsLetter(chars[left]) && Char.IsUpper(chars[left]))
                    {
                        chars[i] = Char.ToLower(chars[i], CultureInfo.InvariantCulture);
                    }
                    else if (Char.IsUpper(chars[i]) && Char.IsLetter(chars[right]) && Char.IsUpper(chars[right]))
                    {
                        chars[i] = Char.ToLower(chars[i], CultureInfo.InvariantCulture);
                    }
                    else if (Char.IsUpper(chars[i]) && !Char.IsLetter(chars[right]))
                    {
                        chars[i] = Char.ToLower(chars[i], CultureInfo.InvariantCulture);
                    }
                }
            }
            chars[chars.Length - 1] = Char.ToLower(chars[chars.Length - 1], CultureInfo.InvariantCulture);
            return new string(chars);
        }

        private string ToPascalCase(string name)
        {
            string notStartingAlpha = Regex.Replace(name, "^[^a-zA-Z]+", "");
            string workingString = ToLowerExceptCamelCase(notStartingAlpha);
            workingString = RemoveSeparatorAndCapNext(workingString);
            return workingString;
        }

        private string RemoveSeparatorAndCapNext(string input)
        {
            string dashUnderscore = "-_";
            string workingString = input;
            char[] chars = workingString.ToCharArray();
            int under = workingString.IndexOfAny(dashUnderscore.ToCharArray());
            while (under > -1)
            {
                chars[under + 1] = Char.ToUpper(chars[under + 1], CultureInfo.InvariantCulture);
                workingString = new String(chars);
                under = workingString.IndexOfAny(dashUnderscore.ToCharArray(), under + 1);
            }
            chars[0] = Char.ToUpper(chars[0], CultureInfo.InvariantCulture);
            workingString = new string(chars);
            return Regex.Replace(workingString, "[-_]", "");
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            //some thing changed
        }

    }
}